﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using Autumn.Common;
using Autumn.NetSession;
using MoonLight.LibCommon;

namespace Autumn.Business
{
    class SvrKernel : Notifier
    {
        public SvrKernel()
        {
            m_sess_mgr = new SessionManager();
            m_acceptor = new Acceptor(ProjectDefine.SeverInfo.ip, ProjectDefine.SeverInfo.port, this);
        }

        public void Run()
        {
            Logger.WriteLog("SvrKernel Start=======" + DateTime.Now.ToString() + "=================");
            ProjectDefine.AcceptorDelegate apt_delegate = new ProjectDefine.AcceptorDelegate(m_acceptor.StartListen);
            apt_delegate.BeginInvoke(new AsyncCallback(m_acceptor.EndListen), null);
#if false
            InfoCollector info_col = new InfoCollector();
            info_col.IpConfig();
            info_col.SystemInfo();
            string text = info_col.GetInfoText();
            MailHandler mail = new MailHandler();
            mail.SetSender("limeng6661985@163.com", "limeng6661985", "madlimeng1985", "smtp.163.com");
            mail.SetReceiver("630240771@qq.com");
            mail.SetMailInfo("Autumn is comming", text);
            mail.SendMail();
#endif
        }

        public void Stop()
        {
            Logger.WriteLog("SvrKernel Shutdown=======" + DateTime.Now.ToString() + "=================");
#if false
            InfoCollector info_col = new InfoCollector();
            string text = info_col.GetInfoText();
            MailHandler mail = new MailHandler();
            mail.SetSender("limeng6661985@163.com", "limeng6661985", "madlimeng1985", "smtp.163.com");
            mail.SetReceiver("630240771@qq.com");
            mail.SetMailInfo("Autumn is gone", text);
            mail.SendMail();
#endif
        }

        public override void handle_message(MessageInfo msg)
        {
            switch ((ProjectDefine.ProjectMsgType_e)msg.msg_type)
            {
                case ProjectDefine.ProjectMsgType_e.MSG_ADD_SESSION:
                    {
                        Logger.WriteLog("create session");
                        Socket handler = (Socket)msg.msg_data;
                        AuSession sess = new AuSession(this);
                        sess.SetSockHandler(handler);
                        m_sess_mgr.AddSession(sess);
                        sess.Start();
                    }
                    break;
                case ProjectDefine.ProjectMsgType_e.MSG_DEL_SESSION:
                    {
                        Logger.WriteLog("delete session");
                        AuSession sess = (AuSession)msg.msg_data;
                        m_sess_mgr.DelSession(sess);
                    }
                    break;
                default:
                    {
                        Logger.WriteLog("unknow msg type [" + (int)msg.msg_type + "]");
                    }
                    break;
            }
        }

        private SessionManager m_sess_mgr;
        private Acceptor m_acceptor;
    }
}
